<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>房源中介管理</title>
    <link rel="stylesheet" type="text/css" href="layui/css/layui.css">
    <style type="text/css">
        .layui-table-cell {
            height: 100%;
            max-width: 100%;
        }
    </style>
</head>
<body>

<form class="layui-form">
    <button type="button" class="layui-btn" id="batchDeleteBtn">
        <i class="layui-icon">&#xe640;</i> 批量删除
    </button>
    <button type="button" class="layui-btn" id="addQuestionBtn">
        <i class="layui-icon">&#xe608;</i> 添加中介
    </button>

    <div class="layui-inline" style="margin-left: 650px">
        <div class="layui-input-inline">
            <input type="text" id="value" style="width: 330px" name="name" lay-verify="required" placeholder="请输入中介姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <button type="button" class="layui-btn" id="search">搜索</button>
</form>

<table id="roomTable" lay-filter="roomTable"></table>


<!-- 添加页面开始 -->
<form class="layui-form" id="addRoomForm" lay-filter="addRoomForm" style="display: none;padding: 20px 55px 0px 0px">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text"  name="name" lay-verify="required" placeholder="请输入姓名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="mobile" lay-verify="required|phone" placeholder="请输入手机号" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="uploadadd">
                <i class="layui-icon">&#xe67c;</i>上传头像
            </button>
            <input type="hidden" name="url" value="" id="url">
            <img src='' height='50px' id="addRoom"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block" >
            <input type="radio" name="sex" checked value="1" title="男">
            <input type="radio" name="sex" value="2" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">工龄</label>
        <div class="layui-input-block">
            <input type="number" name="integral" lay-verify="required|number" placeholder="请输入工龄" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">国家</label>
        <div class="layui-input-block">
            <input type="text"  name="country" lay-verify="required" value="中国" readonly class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="area-picker">
        <div class="layui-form-label">省市</div>
        <div class="layui-input-inline" style="width: 200px;">
            <select name="province" id="province" class="province-selector" lay-verify="required" lay-filter="province-1">

            </select>
        </div>
        <div class="layui-input-inline" style="width: 200px;">
            <select name="city" id="city" class="city-selector" lay-verify="required" lay-filter="city-1">

            </select>
        </div>
    </div>



    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit  lay-filter="addRoomSubmit">立即添加</button>
            <button type="button" id="closeRoomAdd" class="layui-btn layui-btn-primary">关闭</button>
        </div>
    </div>
</form>
<!-- 添加页面结束 -->

<!-- 编辑页面开始 -->
<form class="layui-form" id="amendRoomForm" lay-filter="amendRoomForm" style="display: none;padding: 20px 55px 0px 0px">
    <input type="hidden" id="id" name="id" value="">
    <label class="layui-form-label">姓名</label>
    <div class="layui-input-block">
        <input type="text"  name="name" lay-verify="required" placeholder="请输入姓名" class="layui-input">
    </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="mobile" lay-verify="required|phone"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="amendUpload">
                <i class="layui-icon">&#xe67c;</i>上传头像
            </button>
            <input type="hidden" name="url" value="" id="amendUrl">
            <img src='' height='50px' id="amendRoom"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" lay-verify="required" value="1" title="男">
            <input type="radio" name="sex" lay-verify="required" value="2" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">工龄</label>
        <div class="layui-input-block">
            <input type="number" name="integral" lay-verify="required|number" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">国家</label>
        <div class="layui-input-block">
            <input type="text"  name="country" lay-verify="required" value="中国" readonly class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="area-picker-update">
        <div class="layui-form-label">省市</div>
        <div class="layui-input-inline" style="width: 200px;">
            <select name="province" id="provinceAmend" class="province-selector" lay-verify="required" lay-filter="province-1">

            </select>
        </div>
        <div class="layui-input-inline" style="width: 200px;">
            <select name="city" id="cityAmend" class="city-selector" lay-verify="required" lay-filter="city-1">

            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit  lay-filter="amendRoomSubmit">确认修改</button>
            <button type="button"  id="closeAmendRoom" class="layui-btn layui-btn-primary">关闭</button>
        </div>
    </div>
</form>
<!-- 编辑页面结束 -->



<script type="text/javascript" src="layui/layui.js"></script>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">

    //配置插件目录
    layui.config({
        base: 'layui/mods/'
        , version: '1.0'
    });

    layui.use(['table','form','layer','laydate','upload','layarea'],function () {

        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var layarea = layui.layarea;

        table.render({
            elem:'#roomTable'
            ,url:'house-intermediary/list'
            ,page:true
            ,limit:6
            ,limits:[4,6,8,10]
            ,cols:[[
                {type:'checkbox'}
                ,{field:'id',title:'编号',sort:true,width:80,align:'center'}
                ,{field:'name',title:'姓名',align:'center'}
                ,{field:'mobile',title:'手机号',align:'center'}
                ,{field:'url',title:'头像',align:'center',templet:function (d) {
                        return "<img src='"+d.url+"' height='50px'/>";
                    }}
                ,{field:'sex',title:'性别', templet:function (d) {
                        if (d.sex == 1) {
                            return "男";
                        }else {
                            return "女";
                        }},align:'center'}
                ,{field:'integral',title:'工龄',align:'center'}
                ,{field:'country',title:'国籍',align:'center'}
                ,{field:'province',title:'省份',align:'center'}
                ,{field:'city',title:'城市',align:'center'}
                ,{toolbar: '#barDemo', align:'center'}
            ]]
        });
        /**
         * 监听roomTable的查看编辑删除按钮的单击事件
         * */
        table.on('tool(roomTable)',function (obj) {
            var data = obj.data;
            //编辑按钮的单击事件
            if(obj.event=='edit'){
                form.val("amendRoomForm",data);
                if (data.province ==null || data.city == null) {
                    layarea.render({
                        elem: '#area-picker-update',
                        data: {
                            province: '河南省',
                            city: '郑州市',
                        }
                    });
                }else {
                    layarea.render({
                        elem: '#area-picker-update',
                        data: {
                            province: data.province+'省',
                            city: data.city,
                        }
                    });
                }
                $("#amendRoom").prop("src", data.url);
                layer.open({
                    type:1,
                    title:['编辑房源','font-size:18px;color:orange;'],
                    area:['760px', '80%'],
                    content:$("#amendRoomForm"),
                });
            }
            //删除按钮的单击事件
            if(obj.event=='del'){
                layer.confirm("你确定要删除中介信息吗",{icon:'3',title:'提示'},function () {
                    $.ajax({
                        url:'house-intermediary/delete',
                        data:{
                            id: data.id
                        },
                        type:"delete",
                        dataType:"json",
                        success:function (result) {
                            if(result.code==200){
                                table.reload("roomTable",{
                                    page:{
                                        curr:1
                                    }
                                });
                                layer.msg("删除中介信息成功！",{icon:'1',title:'提示',closeBtn :'1'});
                            }
                        }
                    });
                })
            }
        });
        /**
         * 点击添加按钮弹出层展示添加页面
         * */
        $("#addQuestionBtn").click(function () {
            layarea.render({
                elem: '#area-picker',
                data: {
                    province: '河南省',
                    city: '郑州市',
                }
            });
            layer.open({
                type:1,
                area:['760px','80%'],
                title:['添加中介信息','font-size:18px;color:orange;'],
                content:$("#addRoomForm"),
            });
        });
        /**
         * 渲染添加页面的头像上传
         * */
        upload.render({
            elem:'#uploadadd'
            ,url:'house-intermediary/upload'
            ,done: function(result){
                if(result.code==200){
                    $("#addRoom").prop("src",result.data);
                    $("#url").val(result.data);
                }
            }
        });
        /**
         * 渲染修改页面的头像上传
         * */
        upload.render({
            elem:'#amendUpload'
            ,url:'house-intermediary/upload'
            ,done: function(result){
                if(result.code==200){
                    $("#amendRoom").prop("src",result.data);
                    $("#amendUrl").val(result.data);
                }
            }
        });
        /**
         * 监听添加页面的 立即添加 按钮
         * */
        form.on("submit(addRoomSubmit)",function (obj) {
            layer.confirm("你确定要添加中介信息吗？",{icon:'3',title:'提示'},function () {
                $.ajax({
                    url:'house-intermediary/insert',
                    data:obj.field,
                    type:"post",
                    dataType:"json",
                    success:function (data) {
                        if(data.code==200){
                            obj.form.reset();
                            layer.closeAll();
                            table.reload("roomTable");
                            layer.msg("添加中介信息成功！",{icon:'1',title:'提示',closeBtn :'1'});
                        }
                    }
                });
            });
            return false;
        });

        /**
         * 监听修改页面的确认修改按钮
         * */
        form.on("submit(amendRoomSubmit)",function (obj) {
            layer.confirm("你确定要修改中介信息吗？",{icon:'3',title:'提示'},function () {
                $.ajax({
                    url:'house-intermediary/update',
                    data:obj.field,
                    type:"put",
                    dataType:"json",
                    success:function (data) {
                        if(data.code==200){
                            obj.form.reset();
                            layer.closeAll();
                            table.reload("roomTable");
                            layer.msg("修改中介信息成功！",{icon:'1',title:'提示',closeBtn :'1'});
                        }
                    }
                });
            })
            return false;
        });
        /**
         * 各个页面的一些关闭按钮
         * */
        $("#closeRoomAdd").click(function () {
            layer.closeAll();
        });
        $("#closeAmendRoom").click(function () {
            layer.closeAll();
        })
        /**
         * 搜索功能
         * */
        $("#search").click(function () {
            if($("#value").val()==""){
                table.reload("roomTable",{
                    url:'house-intermediary/list',
                    page:{
                        curr:1
                    }
                });
            }else{
                table.reload("roomTable",{
                    url:'house-intermediary/search',
                    where:{
                        name:$("#value").val()
                    }
                });
            }
        });

        /**
         *批量删除功能
         * */
        $("#batchDeleteBtn").click(function () {
            var checkStatus = table.checkStatus('roomTable');
            if(checkStatus.data.length>0){
                layer.confirm("你确定要删除中介信息吗？",{icon:'3',title:'提示'},function () {
                    var ids = "";
                    for(var i=0;i<checkStatus.data.length;i++){
                        ids += "ids="+checkStatus.data[i].id+"&"
                    }
                    $.ajax({
                        url:"house-intermediary/deleteBatch",
                        data:ids,
                        type:"delete",
                        dataType:"json",
                        success:function (data) {
                            if(data.code==200){
                                table.reload("roomTable",{
                                    page:{
                                        curr:1
                                    }
                                });
                                layer.msg("批量删除中介信息成功！",{icon:'1',title:'提示',closeBtn :'1'});
                            }
                        }
                    });
                })
            }else{
                layer.msg("请至少选择一个中介信息！",{icon:'2',title:'提示',closeBtn :'1'});
            }
        });

        layarea.render({
            elem: '#area-picker',
            // data: {
            //     province: '河南',
            //     city: '郑州市',
            // },
            change: function (res) {
                //选择结果
                $("#province").val(res.province);
                $("#city").val(res.city);
            }
        });

        /**
         * 修改
         */
        layarea.render({
            elem: '#area-picker-update',
            // data: {
            //     province: '河南',
            //     city: '郑州市',
            // },
            change: function (res) {
                //选择结果
                $("#provinceAmend").val(res.province);
                $("#cityAmend").val(res.city);
            }
        });

    });
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

</body>
</html>